package com.redsoil.house.ui.mine.presenter;

import com.redsoil.house.app.AppContext;
import com.redsoil.house.base.RxPresenter;
import com.redsoil.house.model.DataManager;
import com.redsoil.house.model.bean.OnePropertyVO;
import com.redsoil.house.model.http.exception.ApiException;
import com.redsoil.house.model.http.response.HttpResponse;
import com.redsoil.house.ui.mine.contract.FoodsBuyContract;
import com.redsoil.house.ui.mine.contract.MineContract;
import com.redsoil.house.utils.RxUtil;
import com.redsoil.house.widget.CommonSubscriber;

import javax.inject.Inject;

/**
 * 作者：weidingqiang on 2017/9/7 15:39
 * 邮箱：dqwei@iflytek.com
 */

public class FoodsBuyPresenter extends RxPresenter<FoodsBuyContract.View> implements FoodsBuyContract.Presenter {

    private DataManager mDataManager;

    @Inject
    public FoodsBuyPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void attachView(FoodsBuyContract.View view) {
        super.attachView(view);
        registerEvent();
    }

    private void registerEvent(){

    }

    @Override
    public void postOrder(String productId, String price, String count, String customerMobile, String receivingAddr, String receivingName, String buyerMsg, String type, String expressFee) {
        addSubscribe(mDataManager.postOrder(AppContext.getInstance().getToken(),productId,price,count,customerMobile,receivingAddr,receivingName,buyerMsg,type,expressFee)
                .compose(RxUtil.<HttpResponse<String>>rxSchedulerHelper())
                .compose(RxUtil.<String>handleTestResult())
                .subscribeWith(
                        new CommonSubscriber<String>(mView) {
                            @Override
                            public void onNext(String data) {

                                mView.postOrderSuccess();
                            }

                            @Override
                            public void onError(Throwable e) {
                                //当数据返回为null时 做特殊处理
                                try {
                                    int code = ((ApiException) e).getCode();
                                    mView.responeError(e.getMessage());
                                    return;
                                } catch (Exception ex) {

                                }
                                mView.responeError("数据请求失败，请检查网络！");
                            }

                        }
                )
        );
    }
}
